<?php
require_once '../config/database.php';

class Menu {
    private $conn;
    private $table_name = "menu_items";

    public $id;
    public $name;
    public $name_en;
    public $description;
    public $description_en;
    public $price;
    public $category;
    public $image;
    public $available;
    public $spicy_level;
    public $tags;
    public $created_at;
    public $updated_at;

    public function __construct() {
        $database = new Database();
        $this->conn = $database->getConnection();
    }

    // 获取所有菜单项
    public function getAll() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE available = 1 ORDER BY category, name";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 按分类获取菜单项
    public function getByCategory($category) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE category = :category AND available = 1 ORDER BY name";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':category', $category);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 搜索菜单项
    public function search($keyword) {
        $query = "SELECT * FROM " . $this->table_name . " 
                 WHERE (name LIKE :keyword OR description LIKE :keyword) AND available = 1 
                 ORDER BY name";
        $stmt = $this->conn->prepare($query);
        $keyword = "%" . $keyword . "%";
        $stmt->bindParam(':keyword', $keyword);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    // 获取单个菜单项详情
    public function getById($id) {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = :id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }

    // 获取分类列表
    public function getCategories() {
        $query = "SELECT DISTINCT category FROM " . $this->table_name . " WHERE available = 1 ORDER BY category";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_COLUMN);
    }

    // 创建菜单项
    public function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                 (name, name_en, description, description_en, price, category, image, available, spicy_level, tags) 
                 VALUES (:name, :name_en, :description, :description_en, :price, :category, :image, :available, :spicy_level, :tags)";
        
        $stmt = $this->conn->prepare($query);
        
        $stmt->bindParam(':name', $this->name);
        $stmt->bindParam(':name_en', $this->name_en);
        $stmt->bindParam(':description', $this->description);
        $stmt->bindParam(':description_en', $this->description_en);
        $stmt->bindParam(':price', $this->price);
        $stmt->bindParam(':category', $this->category);
        $stmt->bindParam(':image', $this->image);
        $stmt->bindParam(':available', $this->available);
        $stmt->bindParam(':spicy_level', $this->spicy_level);
        $stmt->bindParam(':tags', $this->tags);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    // 更新菜单项
    public function update() {
        $query = "UPDATE " . $this->table_name . " SET 
                 name = :name, name_en = :name_en, description = :description, description_en = :description_en, 
                 price = :price, category = :category, image = :image, available = :available, 
                 spicy_level = :spicy_level, tags = :tags, updated_at = NOW() 
                 WHERE id = :id";
        
        $stmt = $this->conn->prepare($query);
        
        $stmt->bindParam(':name', $this->name);
        $stmt->bindParam(':name_en', $this->name_en);
        $stmt->bindParam(':description', $this->description);
        $stmt->bindParam(':description_en', $this->description_en);
        $stmt->bindParam(':price', $this->price);
        $stmt->bindParam(':category', $this->category);
        $stmt->bindParam(':image', $this->image);
        $stmt->bindParam(':available', $this->available);
        $stmt->bindParam(':spicy_level', $this->spicy_level);
        $stmt->bindParam(':tags', $this->tags);
        $stmt->bindParam(':id', $this->id);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    // 删除菜单项
    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = :id";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(':id', $this->id);
        
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }
}
?>